﻿/**
 * @author Tsingheng
 * @date 2012年9月12日
 * @class ArticleGridPanel
 * @extends Ext.grid.GridPanel
 * @description 文章信息列表表格
 */
ArticleGridPanel = Ext.extend(Ext.grid.GridPanel,{
	myid : "my_id",
	addWin : null,
	editWin : null,
	cateWin : null,
	comboCate : null,
	comboStoreCate : null,	//文章类别下拉框数据源
	keyWordsField : null,
	constructor : function(_cfg){
		if(_cfg == null){
			_cfg = {};
		}
		Ext.apply(this,_cfg);
		this.addWin = new AddArticleWindow();
		//文章类别下拉框数据源
		this.comboStoreCate = new Ext.data.JsonStore({
			url : "category!showCate",
			fields : ["id", "name"],
			autoLoad : true,
			listeners : {
				load : function(store, records, options){
					var data = {"id" : -1, "name" : "查看全部"};
					var rs = [new Ext.data.Record(data)];
					store.insert(0, rs);
				}
			}
		});
		this.comboCate = new Ext.form.ComboBox({
			width : 110,
			store : this.comboStoreCate,
			displayField : "name",
			valueField : "id",
			triggerAction : "all",
			mode : "remote",
			emptyText : "请选择文章分类"
		});
		//数据存储器，即页面表格数据来源
		this["store"] = new Ext.data.JsonStore({
			url : "article!showAllByPage",
			method : "post",
			totalProperty : "totalCount",
			root : "rows",
			fields : ["id", "title", "editor", "category", "addTime", "clickTimes"]
		});
		this.keyWordsField = new Ext.form.TextField({
			allowBlank : true,
			emptyText : "请输入标题关键字"
		});
		// 将父类的构造拷贝过来
		ArticleGridPanel.superclass.constructor.call(this, {
			id : this.myid,
			title : "文章信息列表",
			border : false,
			iconCls : "heng-treeNodeLeafIcon",
			stripeRows : true, //交替行效果
			viewConfig : {
				autoFill : true,
				forceFit : true
			},
			colModel : new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
				header : "文章标题",
				sortable : false,
				dataIndex : "title"
			}, {
				header : "作者",
				sortable : false,
				dataIndex : "editor",
				renderer : function(_value){
					var dataObject = eval(_value);
					return dataObject.realName;
				}
			}, {
				header : "文章分类",
				sortable : true,
				dataIndex : "category",
				renderer : function(_value){
					var dataObject = eval(_value);
					return dataObject.name;
				}
			}, {
				header : "添加时间",
				sortable : true,
				dataIndex : "addTime"
			}, {
				header : "点击次数",
				sortatble : true,
				dataIndex : "clickTimes"
			}]),
			//填充加载时间
			loadMask : {
				msg : "正在加载数据，请稍候..."
			},
			//行选择模式，单行选择
			selModel : new Ext.grid.RowSelectionModel({
				singleSelect : true
			}),
			//顶部工具条
			tbar : ["分类浏览：", this.comboCate, {
				text : "查找",
				tooltip : "根据文章类别查找文章",
				iconCls : "heng-searchicon",	//图标样式
				handler : this.onSearchByCate,	//注册点击事件
				scope : this
			}, "-", "关键字查找：",this.keyWordsField, {
				text : "搜索",
				tooltip : "根据文章标题关键字搜索",
				iconCls : "heng-searchicon",
				handler : this.onSearchByKeyWords,
				scope : this
			}, "-", {
				text : "添加",
				tooltip : "添加文章",
				iconCls : "heng-addicon",
				handler : this.onAddArticle,
				scope : this
			}, "-", {
				text : "修改",
				tooltip : "修改选中的文章",
				iconCls : "heng-editicon",
				handler : this.onEditArticle,
				scope : this
			}, "-", {
				text : "删除",
				tooltip : "删除选中的文章",
				iconCls : "heng-delicon",
				handler : this.onDelArticle,
				scope : this
			}],
			bbar : new Ext.PagingToolbar({
				store : this["store"],
				pageSize : 17,
				displayMsg : "共<font color=\"green\"> {2} </font>条记录,当前页记录索引<font color=\"green\"> {0} - {1}</font>&nbsp;&nbsp;&nbsp;",
				displayInfo : true
			})
		});
		this.getStore().load({
			params : {
				start : 0,
				limit : 17
			}
		});
	},
	/**
	 * 根据文章类别查找
	 */
	onSearchByCate : function(){
		var cate = this.comboCate.getValue();
		if(cate != null && cate != -1){
			//如果有选择类别，按照类别查找
			this["store"].proxy.conn.url = "article!showByCate?cateId=" + this.comboCate.getValue();
			this.getStore().load({
				params : {
					start : 0,
					limit : 17
				}
			});
		}else{
			//如果选择查看全部则查找全部
			this["store"].proxy.conn.url = "article!showAllByPage";
			this.getStore().load({
				params : {
					start : 0,
					limit : 17
				}
			});
		}
		
	},
	/**
	 * 根据标题关键字搜索
	 */
	onSearchByKeyWords : function(){
		this["store"].proxy.conn.url = "article!showByTitle?title=" + encodeURI(encodeURI(this.keyWordsField.getValue()));
		this.getStore().load({
			params : {
				start : 0,
				limit : 17
			}
		});
	},
	/**
	 * 添加文章按钮事件
	 * @memberOf {TypeName} 
	 */
	onAddArticle : function(){
		this.addWin.form.reset();
		this.addWin.show();
	},
	/**
	 * 修改文章按钮事件
	 */
	onEditArticle : function(){
		
	},
	/**
	 * 删除文章按钮事件
	 */
	onDelArticle : function(){
		
	}
});